`:top
In `F33f`_`[class-based`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Class-based]`_`f, `F33f`_`[object-oriented programming`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Object-oriented_programming]`_`f, a `!class variable`! is a `F33f`_`[variable`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Variable_(programming)]`_`f defined in a `F33f`_`[class`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Class_(computer_programming)]`_`f of which a single copy exists, regardless of how many `F33f`_`[instances`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Instance_(computer_science)]`_`f of the class exist.`:cite-ref-thejavaturotialvariables-1-0[`F5bf`_`[1`#cite-note-thejavaturotialvariables-1]`_`f]`:cite-ref-thejavatutorialunderstandinginstanceandclassmembers-2-0[`F5bf`_`[2`#cite-note-thejavatutorialunderstandinginstanceandclassmembers-2]`_`f]`:cite-ref-thepythonlanguagereferencecompoundstatements-3-0[`F5bf`_`[3`#cite-note-thepythonlanguagereferencecompoundstatements-3]`_`f]`:cite-ref-objectivecruntimereference-4-0[`F5bf`_`[4`#cite-note-objectivecruntimereference-4]`_`f]`:cite-ref-classvariablesincsharp-5-0[`F5bf`_`[5`#cite-note-classvariablesincsharp-5]`_`f]
A class variable is not an `F33f`_`[instance variable`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Instance_variable]`_`f. It is a special type of `F33f`_`[class attribute`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Class_(computer_science)]`_`f (or class property, `F33f`_`[field`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Field_(computer_science)]`_`f, or data member). The same dichotomy between `*instance`* and `*class`* members applies to `F33f`_`[methods`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Method_(computer_science)]`_`f ("member functions") as well; a class may have both `F33f`_`[instance methods`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Instance_method]`_`f and `F33f`_`[class methods`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Class_method]`_`f.
>>Contents
• `F0af`_`[Static member variables and static member functions`#static-member-variables-and-static-member-functions]`_`f
• `F0af`_`[Examples`#examples]`_`f
• `F0af`_`[C++`#c]`_`f
• `F0af`_`[Python`#python]`_`f
• `F0af`_`[Notes`#notes]`_`f
-─
>>Static member variables and static member functions
In some languages, class variables and class methods are either statically resolved, not via `F33f`_`[dynamic dispatch`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Dynamic_dispatch]`_`f, or their memory `F33f`_`[statically allocated`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Static_memory_allocation]`_`f at compile time (once for the entire class, as `F33f`_`[static variables`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Static_variable]`_`f), not dynamically allocated at run time (at every instantiation of an object). In other cases, however, either or both of these are dynamic. For example, if classes can be dynamically defined (at run time), class variables of these classes are allocated dynamically when the class is defined, and in some languages class methods are also dispatched dynamically.
Thus in some languages, `!static member variable`! or `!static member function`! are used synonymously with or in place of "class variable" or "class function", but these are not synonymous across languages. These terms are commonly used in `F33f`_`[Java`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Java_(programming_language)]`_`f, `F33f`_`[C#`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=C_Sharp_(programming_language)]`_`f,`:cite-ref-classvariablesincsharp-5-1[`F5bf`_`[5`#cite-note-classvariablesincsharp-5]`_`f] and `F33f`_`[C++`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=C++]`_`f, where class variables and class methods are declared with the `F33f`_`[static keyword`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Static_(keyword)]`_`f, and referred to as `!static member variables`! or `!static member functions`!.
>>Examples
>>>C++
`B100`F9d9struct Request {`f`b
`B100`F9d9`f`b
`B100`F9d9 static int count;`f`b
`B100`F9d9 int number;`f`b
`B100`F9d9`f`b
`B100`F9d9 Requestobject() {`f`b
`B100`F9d9 number = count; // modifies the instance variable "this->number"`f`b
`B100`F9d9 ++count; // modifies the class variable "Request::count"`f`b
`B100`F9d9 }`f`b
`B100`F9d9`f`b
`B100`F9d9};`f`b
`B100`F9d9`f`b
`B100`F9d9int Request::count = 0;`f`b
In this C++ example, the class variable `B100`F9d9Request::count`f`b is `F33f`_`[incremented`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Increment_operator]`_`f on each call to the `F33f`_`[constructor`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Constructor_(computer_science)]`_`f, so that `B100`F9d9Request::count`f`b always holds the number of Requests that have been constructed, and each new Request object is given a `B100`F9d9number`f`b in sequential order. Since `B100`F9d9count`f`b is a class variable, there is only one object `B100`F9d9Request::count`f`b; in contrast, each Request object contains its own distinct `B100`F9d9number`f`b field.
Also note that the variable `B100`F9d9Request::count`f`b is initialized only once.
>>>Python
`B100`F9d9class Dog:`f`b
`B100`F9d9 vertebrate_group = 'mammals' # class variable`f`b
`B100`F9d9`f`b
`B100`F9d9dog_1 = Dog`f`b
`B100`F9d9print(dog_1.vertebrate_group) # accessing the class variable`f`b
In the above Python code, it does not provide much information as there is only class variable in the Dog class that provide the vertebrate group of dog as mammals. In instance variable, you could customize your own object (in this case, dog_1) by having one or more `F33f`_`[instance variables`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Instance_variable]`_`f in the Dog class.
>>Notes
`:cite-note-thejavaturotialvariables-1`!1.`! `F0af`_`[↑`#cite-ref-thejavaturotialvariables-1-0]`_`f "The Java Tutorial, Variables". Retrieved 2010-10-21.
`:cite-note-thejavatutorialunderstandinginstanceandclassmembers-2`!2.`! `F0af`_`[↑`#cite-ref-thejavatutorialunderstandinginstanceandclassmembers-2-0]`_`f "The Java Tutorial, Understanding Instance and Class Members". Retrieved 2010-10-21.
`:cite-note-thepythonlanguagereferencecompoundstatements-3`!3.`! `F0af`_`[↑`#cite-ref-thepythonlanguagereferencecompoundstatements-3-0]`_`f "The Python Language Reference, Compound Statements". Retrieved 2010-10-21.
`:cite-note-objectivecruntimereference-4`!4.`! `F0af`_`[↑`#cite-ref-objectivecruntimereference-4-0]`_`f "Objective-C Runtime Reference". `*Apple Developer`*. Retrieved 1 April 2018.
`:cite-note-classvariablesincsharp-5`!5.`! `F0af`_`[↑`#cite-ref-classvariablesincsharp-5-0]`_`f "Class Variables in C#". `*Syntaxdb`*. Retrieved 1 April 2018.
`c`F0af`_`[↑ Back to top`#top]`_`f`a